package com.sdruixinggroup.mondayb2b.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sdruixinggroup.mondayb2b.R;
import com.sdruixinggroup.mondayb2b.models.HomeBeen;

import java.util.List;

/**
 * Created by Administrator on 2017/12/9.
 */

public class MultiplePagerAdapter extends PagerAdapter{
    private List<HomeBeen.SpecialOfferBean> mList;
    private LayoutInflater layoutInflater;
    private Context context;
    private OnItemClickListener onItemClickListener;

    public MultiplePagerAdapter(List<HomeBeen.SpecialOfferBean> mList, Context context) {
        this.mList = mList;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    /**
     * 页面宽度所占ViewPager测量宽度的权重比例，默认为1
     */
    @Override
    public float getPageWidth(int position) {
        return (float) 0.3;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        ((ViewPager) container).removeView(view);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = layoutInflater.inflate(R.layout.special_shops_adapter_item,container,false);
        ImageView imageView =(ImageView)view.findViewById(R.id.iv_good_pic);
      final   HomeBeen.SpecialOfferBean specialOfferBean= mList.get(position);
        Glide.with(context).load(specialOfferBean.getThumb()).error(R.drawable.home_special_one).into(imageView);
        TextView tvName = (TextView)view.findViewById(R.id.tv_name);
        tvName.setText(specialOfferBean.getName());
        TextView newPrice = (TextView)view.findViewById(R.id.new_price);
        newPrice.setText("¥"+String.valueOf(specialOfferBean.getSale_price()));

        TextView tvOldPrice = (TextView)view.findViewById(R.id.tv_old_price) ;
        tvOldPrice.setText("¥"+specialOfferBean.getOriginal_price());
        tvOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        container.addView(view,0);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != onItemClickListener){
                    onItemClickListener.listener(specialOfferBean);
                }
            }
        });
        return view;
    }

    public void reflushData(List<HomeBeen.SpecialOfferBean> data){
        mList.clear();
        notifyDataSetChanged();
        mList.addAll(data);
        notifyDataSetChanged();

    }


 public interface  OnItemClickListener{
        void listener( HomeBeen.SpecialOfferBean specialOfferBean);
 }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
